unit UDataModule;

interface

uses
  System.SysUtils, System.Classes, Data.DB, Data.Win.ADODB;

type
  TDM = class(TDataModule)
    ADOConnection: TADOConnection;
    qryMemo: TADOQuery;
    qryReminder: TADOQuery;
    qryCategory: TADOQuery;
    dsMemo: TDataSource;
    dsCategory: TDataSource;
    qryTemp: TADOQuery;
    qryMemoID: TAutoIncField;
    qryMemoTitle: TWideStringField;
    qryMemoContent: TMemoField;
    qryMemoCreateTime: TDateTimeField;
    qryMemoModifyTime: TDateTimeField;
    qryMemoCategoryID: TIntegerField;
    qryMemoPriority: TIntegerField;
    qryMemoStatus: TIntegerField;
    qryReminderID: TAutoIncField;
    qryReminderMemoID: TIntegerField;
    qryReminderRemindTime: TDateTimeField;
    qryReminderRemindType: TIntegerField;
    qryReminderRepeatInterval: TIntegerField;
    qryReminderStatus: TIntegerField;
    qryCategoryID: TAutoIncField;
    qryCategoryName: TWideStringField;
    qryCategoryDescription: TWideStringField;
    
    procedure DataModuleCreate(Sender: TObject);
  private
    procedure InitializeConnection;
  public
    function AddMemo(const Title, Content: string; CategoryID: Integer;
      Priority: Integer): Integer;
    procedure UpdateMemo(ID: Integer; const Title, Content: string;
      CategoryID, Priority: Integer);
    procedure DeleteMemo(ID: Integer);
    procedure AddReminder(MemoID: Integer; RemindTime: TDateTime;
      RemindType: Integer);
    procedure PriorityGetText(Sender: TField; var Text: string; DisplayText: Boolean);
    procedure StatusGetText(Sender: TField; var Text: string; DisplayText: Boolean);
  end;

var
  DM: TDM;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}

procedure TDM.DataModuleCreate(Sender: TObject);
begin
  InitializeConnection;
  
  // 设置字段显示格式
  with qryMemo do
  begin
    // 优先级显示转换
    FieldByName('Priority').OnGetText := PriorityGetText;
    // 状态显示转换
    FieldByName('Status').OnGetText := StatusGetText;
  end;
end;

procedure TDM.InitializeConnection;
begin
  with ADOConnection do
  begin
    ConnectionString := 
      'Provider=SQLOLEDB.1;' +
      'Data Source=YourServerName;' +
      'Initial Catalog=MemoDB;' +
      'Integrated Security=SSPI;';
    try
      Connected := True;
    except
      on E: Exception do
        ShowMessage('数据库连接失败: ' + E.Message);
    end;
  end;
end;

function TDM.AddMemo(const Title, Content: string; CategoryID: Integer;
  Priority: Integer): Integer;
begin
  with qryMemo do
  begin
    Close;
    SQL.Text := 'INSERT INTO Memo (Title, Content, CategoryID, Priority, ' +
                'CreateTime, ModifyTime) VALUES (:Title, :Content, ' +
                ':CategoryID, :Priority, GETDATE(), GETDATE()); ' +
                'SELECT SCOPE_IDENTITY() AS ID';
    Parameters.ParamByName('Title').Value := Title;
    Parameters.ParamByName('Content').Value := Content;
    Parameters.ParamByName('CategoryID').Value := CategoryID;
    Parameters.ParamByName('Priority').Value := Priority;
    
    Open;
    Result := FieldByName('ID').AsInteger;
    Close;
  end;
end;

procedure TDM.UpdateMemo(ID: Integer; const Title, Content: string;
  CategoryID, Priority: Integer);
begin
  with qryMemo do
  begin
    Close;
    SQL.Text := 'UPDATE Memo SET Title = :Title, Content = :Content, ' +
                'CategoryID = :CategoryID, Priority = :Priority, ' +
                'ModifyTime = GETDATE() WHERE ID = :ID';
    Parameters.ParamByName('ID').Value := ID;
    Parameters.ParamByName('Title').Value := Title;
    Parameters.ParamByName('Content').Value := Content;
    Parameters.ParamByName('CategoryID').Value := CategoryID;
    Parameters.ParamByName('Priority').Value := Priority;
    ExecSQL;
  end;
end;

procedure TDM.AddReminder(MemoID: Integer; RemindTime: TDateTime;
  RemindType: Integer);
begin
  with qryReminder do
  begin
    Close;
    SQL.Text := 'INSERT INTO Reminder (MemoID, RemindTime, RemindType, Status) ' +
                'VALUES (:MemoID, :RemindTime, :RemindType, 0)';
    Parameters.ParamByName('MemoID').Value := MemoID;
    Parameters.ParamByName('RemindTime').Value := RemindTime;
    Parameters.ParamByName('RemindType').Value := RemindType;
    ExecSQL;
  end;
end;

procedure TDM.PriorityGetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
  case Sender.AsInteger of
    1: Text := '高';
    2: Text := '中';
    3: Text := '低';
  else
    Text := '';
  end;
end;

procedure TDM.StatusGetText(Sender: TField; var Text: string; DisplayText: Boolean);
begin
  case Sender.AsInteger of
    0: Text := '未完成';
    1: Text := '已完成';
  else
    Text := '';
  end;
end;

end. 